package practice_bank;

public class CoupleWithdrawDemo1 {
    public static void main(String[] args) throws InterruptedException {
        // 创建共享账户，初始余额10万元
        JointAccount account = new JointAccount(100000);

        System.out.println("========== 非线程安全取款测试 ==========");
        testWithdraw(account, false);

        // 等待一会儿让测试结果更清晰
        Thread.sleep(1000);

        // 重置账户余额
        account = new JointAccount(100000);

        System.out.println("\n========== 线程安全取款测试 ==========");
        testWithdraw(account, true);
    }

    private static void testWithdraw(JointAccount account, boolean useSafeMethod) throws InterruptedException {
        // 创建两个取款线程
        WithdrawThread xiaoMing = new WithdrawThread(account, 100000, "小明", useSafeMethod);
        WithdrawThread xiaoHong = new WithdrawThread(account, 100000, "小红", useSafeMethod);

        // 启动线程
        xiaoMing.start();
        xiaoHong.start();

        // 等待两个线程结束
        xiaoMing.join();
        xiaoHong.join();

        // 打印最终余额
        System.out.println("最终账户余额: " + account.getBalance() + "元");
    }
}